package com.wsjc.app.log;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.LinkedTransferQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;


/**
 *  功能说明：日志收集 mq listener
 */
public class App 
{
	/*private static final Log LOG = LogFactory.getLog(App.class);

    public static DelayQueue<NotifyTask> tasks = new DelayQueue<NotifyTask>();

    private static ClassPathXmlApplicationContext context;

    private static ThreadPoolTaskExecutor threadPool;

    public static RpNotifyService rpNotifyService;

    public static NotifyPersist notifyPersist;

    public static void main(String[] args) {
        try {
            context = new ClassPathXmlApplicationContext(new String[] { "spring/spring-context.xml" });
            context.start();
            threadPool = (ThreadPoolTaskExecutor) context.getBean("threadPool");
            rpNotifyService = (RpNotifyService) context.getBean("rpNotifyService");

            notifyPersist = (NotifyPersist) context.getBean("notifyPersist");
            notifyPersist.initNotifyDataFromDB();; // 从数据库中取一次数据用来当系统启动时初始化（此处可优化）

            startThread(); // 启动任务处理线程

            LOG.info("== context start");
        } catch (Exception e) {
            LOG.error("== application start error:", e);
            return;
        }
        synchronized (App.class) {
            while (true) {
                try {
                    App.class.wait();
                } catch (InterruptedException e) {
                    LOG.error("== synchronized error:", e);
                }
            }
        }
    }

    private static void startThread() {
        LOG.info("==>startThread");
        threadPool.execute(new Runnable() {
            public void run() {
                try {
                    while (true) {
                    	Thread.sleep(1000);
                        LOG.info("==>threadPool.getActiveCount():" + threadPool.getActiveCount());
                        LOG.info("==>threadPool.getMaxPoolSize():" + threadPool.getMaxPoolSize());
                        LOG.info("一秒间隔==>tasks.size():" + tasks.size());
                        
                        // 如果当前活动线程等于最大线程，那么不执行
                        if (threadPool.getActiveCount() < threadPool.getMaxPoolSize()) {
                            LOG.info("==>tasks.size():" + tasks.size());
                            final NotifyTask task = tasks.take(); //使用take方法获取过期任务,如果获取不到,就一直等待,知道获取到数据
//                            tasks.poll()  // 取出 并删除，有线程安全
                            if (task != null) {
                            	
//                            	threadPool.execute(task)  ;
                            	 
                                threadPool.execute(new Runnable() {
                                    public void run() {
                                        tasks.remove(task);
                                        task.run(); // 执行通知处理
                                        LOG.info("==>tasks.size():" + tasks.size());
                                    }
                                });
                            }
                        }
                    }
                } catch (Exception e) {
                    LOG.error("系统异常;", e);
                }
            }
        });
    }*/
    
    /**
     * 优化 线程的销毁 和 开辟所浪费的资源
     * PS : 2核 的cpu ,最大线程数设置4 ，核心设置为2 ，缓冲队列也是4
     */
    /*private static void startThread() {
        LOG.info("==>startThread");

        for(int i = 0 ; i < threadPool.getMaxPoolSize() ; i ++){
        	
        	threadPool.execute(new Runnable() {
                public void run() {
                    try {
                        while (true) {
                        	Thread.sleep(1500);
                            LOG.info("==>threadPool.getActiveCount():" + threadPool.getActiveCount());
                            LOG.info("==>threadPool.getMaxPoolSize():" + threadPool.getMaxPoolSize());
                            LOG.info("一秒间隔==>tasks.size():" + tasks.size());
                            final NotifyTask task = tasks.take(); //使用take方法获取过期任务,如果获取不到,就一直等待,知道获取到数据
                            if (task != null) {
                                tasks.remove(task);
                                task.run(); // 执行通知处理
                                LOG.info("==>tasks.size():" + tasks.size());
                            }
                        }
                    } catch (Exception e) {
                        LOG.error("系统异常;", e);
                    }
                }
            });

        }
    }*/
    

}
